package com.ssm.pre.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.dao.LifeDao;
import com.ssm.pre.service.LifeService;
import com.ssm.pre.service.WechatOrderService;
import com.ssm.pre.util.DateUtil;
import com.ssm.pre.util.PageCount;
import com.ssm.pre.util.TemplateUtil;
import com.ssm.pre.util.ToolUtil;
import com.ssm.util.object.InputObject;
import com.ssm.util.object.OutputObject;

@Service
public class LifeServiceImpl implements LifeService {

	@Autowired
	private LifeDao lifeDao;
	
	@Autowired
	private WechatOrderService wechatOrderService;

	/**
	 * 查询物业缴费记录
	 */
	@Override
	public void queryPropertyFee(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = lifeDao.queryPropertyFee(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size()); 
		
	}

	/**
	 * 查询电费缴费记录
	 */
	@Override
	public void queryElectricFee(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = lifeDao.queryElectricFee(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size()); 
		
	}

	/**
	 * 微信支付物业费
	 */
	@SuppressWarnings("static-access")
	@Override
	public void addPropertyFee(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		Map<String, Object> wechatParams = inputObject.getWechatLogParams();
		System.out.println("."+params);
		System.out.println(".."+wechatParams);
		//生成订单编号
		String str1 = DateUtil.getUUID();//随机生成的6位数
		String str2 = DateUtil.getUUID();//随机生成的6位数
		String str3 = DateUtil.getTimeToString(); //当月时间 20170722
		String str4 = DateUtil.getUUID();;//随机生成的6位数
		String orderNumber = str1+str2+str3+str4;//拼接订单号	
		double orderPrice = Double.parseDouble((String)params.get("propertyFee"));//总价格
		String openid = wechatParams.get("openId").toString();
		
		Map<String, Object> orderParams = new HashMap<String, Object>();
		orderParams.put("orderNumber", orderNumber);//订单编号
		orderParams.put("createId", wechatParams.get("id"));//创建人
		orderParams.put("createTime", ToolUtil.getTimeAndToString());//创建时间
		orderParams.put("orderPrice", orderPrice);//订单总价格
		orderParams.put("wetherPayment", 0);//支付状态初始值为0
		orderParams.put("orderType", 1);//订单类型：物业费订单
		lifeDao.addLifeFee(orderParams);//添加物业费记录
		
		int orderFee = (int)(orderPrice * 100);
		//调用支付接口
		Map<String, Object> payParams = new HashMap<String,Object>();
		payParams.put("openid", openid);
		payParams.put("out_trade_no", orderNumber);//订单编号
		payParams.put("orderFee", orderFee);//支付金额
		payParams.put("orderNumber", orderNumber);//订单编号
		payParams.put("orderId", orderParams.get("id"));//订单ID
		inputObject.setParams(payParams);
		wechatOrderService.getOrderParameter(inputObject, outputObject);
	}

	/**
	 * 更改物业费支付的状态
	 */
	@Override
	public void updatePropertyState(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		Map<String, Object> wechatParams = inputObject.getWechatLogParams();
		
		params.put("wetherPaymentTime", ToolUtil.getTimeAndToString());//支付时间
		params.put("orderType", 1);
		lifeDao.updateOrderState(params);//更改订单状态
		System.out.println("......params...."+params);
		System.out.println("wechatParams"+wechatParams);
		
		//获取当前时间，即缴费日期
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		//新增物业缴费记录到物业费表(itcast_propertyFee)中
		wechatParams.put("date", sdf.format(d));
		wechatParams.put("starttime", params.get("startTime").toString());
		wechatParams.put("endtime", params.get("endTime").toString());
		wechatParams.put("moneyMin", params.get("payMoney").toString());
		wechatParams.put("span", params.get("span").toString());//物业费有效期限
		wechatParams.put("feeType","微信缴费");
		wechatParams.put("companyname", "周口鼎兴物业服务有限公司");
		wechatParams.put("propertyUnitPrice", "1");
		wechatParams.put("agenttel", "0394-8581688");
		wechatParams.put("position", params.get("position").toString());
		System.out.println("***property="+wechatParams);
		//根据房号增加物业费表中的信息
		lifeDao.addPropertyFeeByaccountNumber(wechatParams);
		
		/*********************************************************************/
		//将支付金额添加到OA的物业费表中
		/*wechatParams.put("startTime", params.get("startTime").toString());
		wechatParams.put("endTime", params.get("endTime").toString());
		wechatParams.put("payMoney", params.get("payMoney").toString());
		wechatParams.put("proTime", params.get("proTime").toString());//时间戳
		wechatParams.put("feeType","微信缴费");
		//根据房号，添加一条缴费记录
		lifeDao.addJiaoFeiByHouseNumber(wechatParams);
		//缴费明细表
		lifeDao.addJiaoFeiMxByHouseNumber(wechatParams);*/
		/*********************************************************************/
		
		wechatParams.put("orderType", "物业费");
		wechatParams.put("nowDate",sdf.format(d));//缴费日期
		wechatParams.put("payFee", params.get("payMoney").toString());//缴费金额
		System.out.println("+++property_wechatParams="+wechatParams);
		//发送模板消息
		TemplateUtil.Template4(wechatParams);
	}

	/**
	 * 微信端查询物业费账单
	 */
	@Override
	public void queryWechatPropertyByList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getWechatLogParams();
		System.out.println("微信端查询物业费缴费记录：...params:"+params);
		//根据accountNumber（3#-1-101  3号楼1单元101室）查找物业费记录
		List<Map<String, Object>> propertList = lifeDao.queryWechatPropertyByList(params);
		if(propertList.size() != 0){
			outputObject.setBeans(propertList);
		}else{
			outputObject.setreturnMessage("暂无缴费信息！");
		}
		
	}

	/**
	 * 微信端查询电费账单
	 */
	@Override
	public void queryWechatElectricByList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getWechatLogParams();
		
		//根据houseNumber（3#-1-101  1号楼3单元101室）查找物业费记录
		List<Map<String, Object>> propertList = lifeDao.queryWechatElectricByList(params);
		if(propertList.size() != 0){
			outputObject.setBeans(propertList);
		}else{
			outputObject.setreturnMessage("暂无缴费信息！");
		}
	}

	/**
	 * 微信支付电费
	 */
	@SuppressWarnings("static-access")
	@Override
	public void addEclectricFee(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		Map<String, Object> wechatParams = inputObject.getWechatLogParams();
		System.out.println("."+params);
		System.out.println(".."+wechatParams);
		//生成订单编号
		String str1 = DateUtil.getUUID();//随机生成的6位数
		String str2 = DateUtil.getUUID();//随机生成的6位数
		String str3 = DateUtil.getTimeToString(); //当月时间 20170722
		String str4 = DateUtil.getUUID();;//随机生成的6位数
		String orderNumber = str1+str2+str3+str4;//拼接订单号	
		double orderPrice = Double.parseDouble((String)params.get("preElectricFee"));//预交的电费金额
		String openid = wechatParams.get("openId").toString();
		
		Map<String, Object> orderParams = new HashMap<String, Object>();
		orderParams.put("orderNumber", orderNumber);//订单编号
		orderParams.put("createId", wechatParams.get("id"));//创建人
		orderParams.put("createTime", ToolUtil.getTimeAndToString());//创建时间
		orderParams.put("orderPrice", orderPrice);//订单总价格
		orderParams.put("wetherPayment", 0);//支付状态初始值为0
		orderParams.put("orderType", 2);//订单类型：电费订单
		lifeDao.addLifeFee(orderParams);//添加电费记录
		
		int orderFee = (int)(orderPrice * 100);
		//调用支付接口
		Map<String, Object> payParams = new HashMap<String,Object>();
		payParams.put("openid", openid);
		payParams.put("out_trade_no", orderNumber);//订单编号
		payParams.put("orderFee", orderFee);//支付金额
		payParams.put("orderNumber", orderNumber);//订单编号
		payParams.put("orderId", orderParams.get("id"));//订单ID
		inputObject.setParams(payParams);
		wechatOrderService.getOrderParameter2(inputObject, outputObject);
		
	}

	/**
	 * 微信端根据房屋编号(3#-1-102)查询用电量
	 */
	@Override
	public void queryEclectricFeeByNumber(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		Map<String, Object> bean = lifeDao.queryEclectricFeeByNumber(map);
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("当前时间：" + sdf.format(d));
		
		if(bean != null){
			bean.put("nowTime", sdf.format(d));
			outputObject.setBean(bean);
		}else{
			outputObject.setreturnMessage("暂无缴费信息！");
		}
		
		
	}

	/**
	 * 更改电费的支付状态
	 */
	@Override
	public void updateEclectricState(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params =inputObject.getParams();
		Map<String, Object> wechatParams = inputObject.getWechatLogParams();
		params.put("wetherPaymentTime", ToolUtil.getTimeAndToString());//支付时间
		params.put("orderType", 2);
		
		lifeDao.updateOrderState(params);
		System.out.println("params"+params);
		System.out.println("wechatParams"+wechatParams);
		
		wechatParams.put("nowDate", params.get("nowDate"));
		wechatParams.put("JieyuMoney", params.get("JieyuMoney").toString());
		wechatParams.put("totalQianMoney", params.get("totalQianMoney").toString());
		wechatParams.put("preElectricity", params.get("preElectricFee").toString());
		wechatParams.put("electricType", "微信缴费");
		wechatParams.put("moneyMin", params.get("moneyMin").toString());
		System.out.println("--wechatParams="+wechatParams);
		//根据房号更改电费表中的缴费日期，结余金额以及预交金额
		lifeDao.updateElectricByHouseNumber(wechatParams);
		wechatParams.put("orderType", "电费");
		wechatParams.put("payFee", params.get("preElectricFee"));
		System.out.println("---electric_wechatParams="+wechatParams);
		//发送模板消息
		TemplateUtil.Template4(wechatParams);
	}

}
